package com.joelbitar.switchctrl;

import android.content.ContentValues;
import android.content.Context;
import android.database.*;
import android.database.sqlite.*;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class DeviceDbAdapter {
	private static final String DATABASE_NAME = "switchctrl.db";
	private static final String DATABASE_TABLE = "servers";
	private static final int DATABASE_VERSION = 1;
	
	// The index key
	public static final String KEY_ID = "_id";
	public static final int ID_COLUMN = 0;
	
	// Names of columns

	public static final String KEY_NAME = "name";
	public static final int NAME_COLUMN = 1;
	
	public static final String KEY_STATUS = "status";
	public static final int STATUS_COLUMN = 2;
	
	public static final String KEY_SERVER_ID = "server";
	public static final int SERVER_ID_COLUMN = 3;
	
	public static final String KEY_TYPE = "type";
	public static final int TYPE_COLUMN = 4;
	
	public static final String KEY_DEVICE_ID = "deviceid";
	public static final int DEVICE_ID_COLUMN = 5;
	
	public static final String KEY_ICON = "icon";
	public static final int ICON_COLUMN = 6;
	
	public static final String KEY_DESC = "description";
	public static final int DESC_COLUMN = 7;
	
	public static final String[] ALL_KEYS = {KEY_NAME, KEY_STATUS, KEY_SERVER_ID, KEY_TYPE, KEY_DEVICE_ID, KEY_ICON, KEY_DESC};
	
	private static final String DATABASE_CREATE = "create table " + 
		DATABASE_TABLE + 
		" (" + KEY_ID + " integer primary key autoincrement, " + 
		KEY_SERVER_ID + " integer not null, " + 
		KEY_STATUS + 	" integer not null, " +
		KEY_TYPE + 		" integer not null, " +
		KEY_DEVICE_ID + " integer not null, " +
		KEY_ICON + 		" integer not null, " +
		KEY_NAME + 		" text not null, " +
		KEY_DESC + 		" text not null, " +
		KEY_STATUS + 	" boolean);";
	
	private DeviceDbHelper dbHelper;
	
	private SQLiteDatabase db;

	private final Context context;
	
	public DeviceDbAdapter(Context context) {
		this.context = context;
		this.dbHelper = new DeviceDbHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
		
		// Trying to get a writable database, if not, get a readable database.
		try {
			db = dbHelper.getWritableDatabase();
		} 
		catch (SQLiteException e) {
			db = dbHelper.getReadableDatabase();
		}
	}
	
	public void close() {
		db.close();
	}
	
	private ContentValues getContentValuesFromDevice(Device device) {

		ContentValues contentValues = new ContentValues();
		// TODO Insert real content values
		contentValues.put(KEY_NAME, device.getName());
		contentValues.put(KEY_STATUS, device.getStatus());
		contentValues.put(KEY_SERVER_ID, device.getServer().getId());
		contentValues.put(KEY_TYPE, device.getType());
		contentValues.put(KEY_DEVICE_ID, device.getId());
		contentValues.put(KEY_ICON, device.getIcon());
		contentValues.put(KEY_DESC, device.getDesc());
		
		Log.w("contentvalues", "Getting Content Values");
		
		return contentValues;
	}
	
	public long insertDevice(Device device) {
		
		ContentValues contentValues = getContentValuesFromDevice(device);
		
		return db.insert(DATABASE_TABLE, null, contentValues);
	}
	
	// TODO remove entry
	
	
	public Cursor getAllDevices() {
		return db.query(DATABASE_TABLE, ALL_KEYS, null, null, null, null, null);
	}

	public Device getDevice(long index) {
		
		Log.i("gettingserver", "Getting device");
		Log.i("id", "" + index);
		
		Device device = new Device();

		Log.i("gettingdevice", "blah");
		
		Cursor cursor = db.query(DATABASE_TABLE, ALL_KEYS , KEY_ID + " = " + index, null, null, null, null);

		Log.i("cursorcount", "" + cursor.getCount());
		
		if((cursor.getCount() == 0) || !cursor.moveToFirst()){
			throw new SQLException ("No server found with ID: " + index);
		}
		
		device.setFromCursor(cursor);
		
		return device;
	}

	public Server getDeviceById(long index) {
		
		Log.i("gettingserver", "Getting device");
		Log.i("id", "" + index);
		
		Server result = new Server();
		
		Log.i("gettingdevice", "blah");
		
		Cursor cursor = db.query(DATABASE_TABLE, ALL_KEYS , KEY_DEVICE_ID + " = " + index, null, null, null, null);

		Log.i("cursorcount", "" + cursor.getCount());
		
		if((cursor.getCount() == 0) || !cursor.moveToFirst()){
			throw new SQLException ("No server found with ID: " + index);
		}
		
		result.setFromCursor(cursor);
		
		return result;
	}
	
	public int updateServer(Device device) {
		String where = KEY_ID + "=" + device.getId();
		
		Log.i("updateserver", "UPDATE");
		Log.i("updateserver", where);
		
		ContentValues contentValues = getContentValuesFromDevice(device);
		
		return db.update(DATABASE_TABLE, contentValues, where, null);
	}
	
	
	
	private static class DeviceDbHelper extends SQLiteOpenHelper {
		public DeviceDbHelper(Context context, String name, CursorFactory factory, int version) {
			super(context, name, factory, version);
		}
		
		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DATABASE_CREATE);
		}
		
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			
			Log.w("ServerAdapter", "UPPGRADING");
			
			// TODO real update shoudl be done
			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
			
			onCreate(db);
		}
	}

	public void deleteDevice(Device device) {
		// TODO Auto-generated method stub
		db.delete(DATABASE_TABLE, KEY_ID + " = " + device.getId(), null);
	}
}
